﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;
using System.Web.Mvc;
using Core;

namespace Controllers
{
    public class SitemapController : BaseController
    {

        public ActionResult Xml()
        {
            var mynewxml = Getxml();
            return Content(mynewxml, "text/xml", Encoding.UTF8);
        }
        protected string Getxml()
        {
            var myurl = "http://" + Request.Url.DnsSafeHost;
            var changeline = "\r\n";
            var mynewxml = string.Empty;
            if (string.IsNullOrEmpty(Com.CacheHelper.Get<string>("sitemap")))
            {
                mynewxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + changeline;
                mynewxml += "<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84/\">" + changeline;
                //添加首页
                mynewxml += "<url>" + changeline;
                mynewxml += "<loc><![CDATA[" + myurl + "]]></loc>" + changeline;
                mynewxml += "<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>" + changeline;
                mynewxml += "<priority>1.0</priority>" + changeline;
                mynewxml += "<changefreq>weekly</changefreq>" + changeline;
                mynewxml += "</url>" + changeline;
                var categoryCore = new categoryCore();
                var catelist = categoryCore.GetList(0, "", "ID desc");
                foreach (var cate in catelist)
                {
                    mynewxml += "<url>" + changeline;
                    mynewxml += "<loc><![CDATA[" + myurl + "/list/" + cate.ID + "]]></loc>" + changeline;
                    mynewxml += "<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>" + changeline;
                    mynewxml += "<priority>0.8</priority>" + changeline;
                    mynewxml += "<changefreq>weekly</changefreq>" + changeline;
                    mynewxml += "</url>" + changeline;
                }

                var atricleCore = new categoryCore();
                var page = 1;
                var pagesize = 100;
                var atriclelist = atricleCore.GetPage(page, pagesize, "", "ID desc");
                var pages = atriclelist.TotalPages;
                for (; page <= pages; page++)
                {
                    if (page > 1)
                    {
                        atriclelist = atricleCore.GetPage(page, pagesize, "", "ID desc");
                    }

                    foreach (var atricle in atriclelist.Items)
                    {
                        mynewxml += "<url>" + changeline;
                        mynewxml += "<loc><![CDATA[" + myurl + "/atricle/" + atricle.ID + "]]></loc>" + changeline;
                        mynewxml += "<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>" + changeline;
                        mynewxml += "<priority>0.8</priority>" + changeline;
                        mynewxml += "<changefreq>weekly</changefreq>" + changeline;
                        mynewxml += "</url>" + changeline;
                    }
                }

                mynewxml += "</urlset>";
                Com.CacheHelper.Insert("sitemap", mynewxml, 60 * 24 * 7);
            }
            else
            {
                mynewxml = Com.CacheHelper.Get<string>("sitemap");
            }

            return mynewxml;
        }
    }
}
